<template>
    <h2 style="text-align: center;">权益列表</h2>
    <a href="/AddMemberRight" class="btn btn-success">添加权益信息</a><br>

    <table border="1" class="table table-bordered">
        <thead style="background-color: aquamarine;">
            <tr style="text-align: center;">
                <th>权益编号</th>
                <th>权益名称</th>
                <th>权益说明</th>
                <th>权益状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody style="text-align: center;">
            <tr v-for="itme in Info">
                <td>{{ itme.MemberRight_Id }}</td>
                <td>{{ itme.MemberRight_Name }}</td>
                <td>{{ itme.MemberRight_Info }}</td>
                <td v-show="itme.MemberRight_State==true" style="color: green;">启用</td>
                <td v-show="itme.MemberRight_State==false" style="color: red;">禁用</td>
                <td>
                    <a @click="UpdRightState(itme.MemberRight_Id)" class="btn btn-success" style="margin-right: 10px;">修改状态</a>
                    <a @click="UpdMembeRight(itme.MemberRight_Id)" class="btn btn-success" style="margin-right: 10px;">修改</a>
                    <a @click="DelMemberRight(itme.MemberRight_Id)" class="btn btn-danger" style="margin-right: 10px;">删除</a>
                </td>
            </tr>
        </tbody>
    </table>
    <span>
        数据共{{ PageInfo.zongtiao }}条，每页{{ PageInfo.size }}条，共{{ PageInfo.index }}/{{ PageInfo.zongpage }}页

        <a @click="FanYe('F')" class="btn btn-success" style="margin-right: 10px;">首页</a>
        <a @click="FanYe('U')" class="btn btn-success" style="margin-right: 10px;">上一页</a>
        <a @click="FanYe('N')" class="btn btn-success" style="margin-right: 10px;">下一页</a>
        <a @click="FanYe('E')" class="btn btn-success" style="margin-right: 10px;">尾页</a>

    </span>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import { useRouter } from 'vue-router';

    const router=useRouter();
    //显示响应式数据
    const Info=ref([{
        MemberRight_Id: 0,
        MemberRight_Name: "",
        MemberRight_Info: "",
        MemberRight_State: true,
    }])
    //分页响应式数据
    const PageInfo=ref({
        index:1,
        size:2,
        zongtiao:0,
        zongpage:0,
    })
    //翻页
    const FanYe=(chooes:string)=>{
        switch(chooes)
        {
            case"F":
                PageInfo.value.index=1;
            break;
            case"U":
                if(PageInfo.value.index>1)
                {
                    PageInfo.value.index--;
                }
                else
                {
                    alert('当前已是第一页！');
                    return;
                }
            break;
            case"N":
                if(PageInfo.value.index<PageInfo.value.zongpage)
                {
                    PageInfo.value.index++;
                }
                else
                {
                    alert('当前已是最后一页！');
                    return;
                }
            break;
            case"E":
                PageInfo.value.index=PageInfo.value.zongpage;
            break;
        }
        ShowMemberRight();//显示
    }
    onMounted(()=>{
        ShowMemberRight();//显示
    })
    //显示
    const ShowMemberRight=()=>{
        axios.get("https://localhost:44390/api/MemberRight/ShowMemberRight",
            {
                params:{
                    index:PageInfo.value.index,
                    size:PageInfo.value.size,
                }
            }
        )
        .then(res=>{
            Info.value=res.data.Plist,
            PageInfo.value.zongtiao=res.data.ZongTiao,
            PageInfo.value.zongpage=res.data.ZongPage
        })
    }
    //修改状态
    const UpdRightState=(MemberRight_Id:number)=>{
        if(confirm("确定修改？")){
            axios.put("https://localhost:44390/api/MemberRight/UpdRightState?id="+MemberRight_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('修改成功');
                    ShowMemberRight();//显示
                }
                else
                {
                    alert('修改失败');
                }
            })
        }
    }
    //修改
    const UpdMembeRight=(MemberRight_Id:number)=>{
        router.push({
            name:"UpdMembeRight",
            params:{
                id:MemberRight_Id
            }
        })
    }
    //删除
    const DelMemberRight=(MemberRight_Id:number)=>{
        if(confirm("确定删除?")){
            axios.delete("https://localhost:44390/api/MemberRight/DelMemberRight?id="+MemberRight_Id)
            .then(res=>{
                if(res.data>0)
                {
                    alert('删除成功');
                    ShowMemberRight();//显示
                }
                else
                {
                    alert('删除失败');
                }
            })
        }
    }
</script>